package com.ruoyi.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.logistic.domain.NetworkEntryFeeSettle;
import com.ruoyi.logistic.domain.NetworkEntryFeeSettleCollect;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.logistic.domain.vo.NetworkEntryFeeSettleCollectVo;
import com.ruoyi.logistic.domain.vo.NetworkEntryFeeSettleVo;
import com.ruoyi.logistic.mapper.NetworkEntryFeeSettleCollectMapper;
import com.ruoyi.logistic.mapper.NetworkEntryFeeSettleMapper;
import com.ruoyi.logistic.service.NetworkEntryFeeSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.List;

@Service
public class NetworkEntryFeeSettleServiceImpl implements NetworkEntryFeeSettleService {
    @Autowired
    private NetworkEntryFeeSettleMapper networkEntryFeeSettleMapper;
    @Autowired
    private NetworkEntryFeeSettleCollectMapper networkEntryFeeSettleCollectMapper;

    @Override
    public List<NetworkEntryFeeSettle> selectAll(PageDate pageDate) {
        PageHelper.startPage(pageDate);
        return networkEntryFeeSettleMapper.selectAll();
    }

    @Override
    public NetworkEntryFeeSettle findById(Integer id) {
        return networkEntryFeeSettleMapper.findById(id);
    }

    @Override
    public Boolean insert(NetworkEntryFeeSettle networkEntryFeeSettle) {
        if (networkEntryFeeSettle.getSignTime() != null && networkEntryFeeSettle.getArriveTime() != null){
            Timestamp signTime = new Timestamp(networkEntryFeeSettle.getSignTime().getTime());
            Timestamp arriveTime = new Timestamp(networkEntryFeeSettle.getArriveTime().getTime());
            Long differenceInMilliseconds = Math.abs(signTime.getTime() - arriveTime.getTime());
            System.out.println(differenceInMilliseconds);
            DecimalFormat df = new DecimalFormat("#.##");
            String formattedDouble = df.format(differenceInMilliseconds / (1000.0 * 60.0 * 60.0));
            networkEntryFeeSettle.setDeliverTimeLast(Double.parseDouble(formattedDouble));
        }
        return networkEntryFeeSettleMapper.insert(networkEntryFeeSettle);
    }

    @Override
    public Boolean update(NetworkEntryFeeSettle networkEntryFeeSettle) {
        if (networkEntryFeeSettle.getSignTime() != null && networkEntryFeeSettle.getArriveTime() != null){
            Timestamp signTime = new Timestamp(networkEntryFeeSettle.getSignTime().getTime());
            Timestamp arriveTime = new Timestamp(networkEntryFeeSettle.getArriveTime().getTime());
            Long differenceInMilliseconds = Math.abs(signTime.getTime() - arriveTime.getTime());
            System.out.println(differenceInMilliseconds);
            DecimalFormat df = new DecimalFormat("#.##");
            String formattedDouble = df.format(differenceInMilliseconds / (1000.0 * 60.0 * 60.0));
            System.out.println("111111:" + Double.parseDouble(formattedDouble));
            networkEntryFeeSettle.setDeliverTimeLast(Double.parseDouble(formattedDouble));
        }
        networkEntryFeeSettle.setUpdateBy(SecurityUtils.getUserId());
        return networkEntryFeeSettleMapper.update(networkEntryFeeSettle);
    }

    @Override
    public Boolean deleteById(Integer id) {
        return networkEntryFeeSettleMapper.deleteById(id);
    }

    @Override
    public List<NetworkEntryFeeSettle> search(NetworkEntryFeeSettleVo vo) {
        PageHelper.startPage(vo.getPageDate());
        return networkEntryFeeSettleMapper.search(vo);
    }

    @Override
    public List<NetworkEntryFeeSettleCollect> collect(NetworkEntryFeeSettleCollectVo vo) {
        PageHelper.startPage(vo.getPageDate());
        List<NetworkEntryFeeSettleCollect> collects = networkEntryFeeSettleCollectMapper.collect(vo);
        assert collects != null;
        for (NetworkEntryFeeSettleCollect collect : collects){
            if (vo.getTimeType() == 2){
                collect.setCollectDate(vo.getStartTime() + "至" + vo.getEndTime());
            }
        }
        return collects;
    }


}
